﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using IndustrialSmokeCalc.SmokeCalcWCF;
using Telerik.Windows.Controls;

namespace IndustrialSmokeCalc
{
    public partial class Form : PhoneApplicationPage
    {
        private MeasurementDBDataContext _measurementDBDataContext;

        public Form()
        {
            InitializeComponent();
            _measurementDBDataContext = new MeasurementDBDataContext(MeasurementDBDataContext.DBConnectionString);

            // Data context and observable collection are children of the main page.
            this.DataContext = this;
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NavigationService.RemoveBackEntry();
            NavigationService.RemoveBackEntry();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (!txtObserver.Text.IsEmpty())
            {
                SmokeCalcClient client = new SmokeCalcClient();
                client.AddMeasurementCompleted += AddMeasurementCompleted;
                client.AddMeasurementAsync(DataSingleton.Instance.ChimneyId, DataSingleton.Instance.UserId,
                    txtObserver.Text, GetCurrentDate(),
                    DataSingleton.Instance.StartTime, DataSingleton.Instance.FinishTime,
                    DataSingleton.Instance.RingelmannOpacity.ToString(),
                    txtUnitAddr.Text, txtEmissionAddr.Text, txtHeight.Text, txtRelativeHeight.Text, txtDistance.Text,
                    txtDir1.Text,
                    txtDir2.Text, txtEmissionDes1.Text, txtEmissionDes2.Text, txtEmissionColor1.Text,
                    txtEmissionColor2.Text, txtPoint1.Text,
                    txtPoint2.Text, txtBgDes1.Text, txtBgDes2.Text, txtBgColor1.Text, txtBgColor2.Text, txtSky1.Text,
                    txtSky2.Text,
                    txtWind1.Text, txtWind2.Text, txtWindDir1.Text, txtWindDir2.Text, txtTemp1.Text, txtTemp2.Text,
                    txtWetTemp.Text, txtRh.Text, DataSingleton.Instance.StringOpacity);
            }
            else MessageBox.Show("Nama pengamat tidak boleh kosong", "Peringatan", MessageBoxButton.OK);
        }

        private void AddMeasurementCompleted(object sender, AddMeasurementCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                InsertDraft();
            }
            else if (e.Result == "sukses")
            {
                MessageBox.Show("Data formulir pengamatan telah diunggah ke server.");
                NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show(e.Result);
            }
        }

        private string GetCurrentDate()
        {
            DateTime now = DateTime.Now;
            return String.Format(now.ToString("dd/MM/yyyy"));
        }

        private void ShowExceptionMessage()
        {
            MessageBox.Show("Tidak dapat melakukan koneksi ke server.\r\nData pengamatan telah disimpan pada basis data lokal.");
            NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
        }

        private void InsertDraft()
        {
            MeasurementDB m = new MeasurementDB();
            m.ChimneyId = DataSingleton.Instance.ChimneyId;
            m.UserId = DataSingleton.Instance.UserId;
            m.MeasurementObserver = txtObserver.Text;
            m.MeasurementDate = GetCurrentDate();
            m.MeasurementTime1 = DataSingleton.Instance.StartTime;
            m.MeasurementTime2 = DataSingleton.Instance.FinishTime;
            m.MeasurementResultRingelmann = DataSingleton.Instance.RingelmannOpacity.ToString();
            m.MeasurementUnitAddr = txtUnitAddr.Text;
            m.MeasurementEmissionAddr = txtEmissionAddr.Text;
            m.MeasurementHeight = txtHeight.Text;
            m.MeasurementRelativeHeight = txtRelativeHeight.Text;
            m.MeasurementDistance = txtDistance.Text;
            m.MeasurementDir1 = txtDir1.Text;
            m.MeasurementDir2 = txtDir2.Text;
            m.MeasurementEmissionDes1 = txtEmissionDes1.Text;
            m.MeasurementEmissionDes2 = txtEmissionDes2.Text;
            m.MeasurementEmissionColor1 = txtEmissionColor1.Text;
            m.MeasurementEmissionColor2 = txtEmissionColor2.Text;
            m.MeasurementPoint1 = txtPoint1.Text;
            m.MeasurementPoint2 = txtPoint2.Text;
            m.MeasurementBgDes1 = txtBgDes1.Text;
            m.MeasurementBgDes2 = txtBgDes2.Text;
            m.MeasurementBgColor1 = txtBgColor1.Text;
            m.MeasurementBgColor2 = txtBgColor2.Text;
            m.MeasurementSky1 = txtSky1.Text;
            m.MeasurementSky2 = txtSky2.Text;
            m.MeasurementWind1 = txtWind1.Text;
            m.MeasurementWind2 = txtWind2.Text;
            m.MeasurementWindDir1 = txtWindDir1.Text;
            m.MeasurementWindDir2 = txtWindDir2.Text;
            m.MeasurementTemp1 = txtTemp1.Text;
            m.MeasurementTemp2 = txtTemp2.Text;
            m.MeasurementWetTemp = txtWetTemp.Text;
            m.MeasurementRh = txtRh.Text;
            m.MeasuremenResulttDetail = DataSingleton.Instance.StringOpacity;

            _measurementDBDataContext.MeasurementTable.InsertOnSubmit(m);
            _measurementDBDataContext.SubmitChanges();
            ShowExceptionMessage();
        }

        private void btnDraft_Click(object sender, RoutedEventArgs e)
        {
            InsertDraft();
        }
    }
}